package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.common.util.ConstantUtil;
import com.cogent.common.util.Globals;
import com.cogent.common.util.MapUtil;
import com.cogent.dao.BussinessInstance;
import com.cogent.dao.BussinessQueryHelper;

import com.cogent.dao.facade.BussinessDaoFacade;
import com.opensymphony.oscache.util.StringUtil;

public class DistrictHelper {
	public static HashMap getDistByCode(String code) {
		String sql = "select * from sys_district where districtcode='" + code + "' and rownum<=1";
		HashMap curMap = BussinessQueryHelper.queryOneRow("sys_district",sql);
		
		
		return curMap;
	}
	
	public static HashMap getDistByID(String id) {
		String sql = "select * from sys_district where id='" + id + "'";
		HashMap curMap = BussinessQueryHelper.queryOneRow("sys_district",sql);
		
		
		return curMap;
	}
	
	public static List getSubDist(String parentid) {
		String sql = "select * from sys_district where parentid='" + parentid + "'";
		List list = BussinessQueryHelper.query("sys_district", sql);
		
		return list;
	}
	
	public static List getSubDistOptionList(HashMap formMap, String parentid, String curid, boolean isfirst) {
		List list = new ArrayList();
		if((StringUtil.isEmpty(parentid)) && (!isfirst))
			return list;
		if(StringUtil.isEmpty(parentid)) {
			HashMap curMap = DistrictHelper.getDistByCode(Globals.getProperty("syssetting.defdistrictcode"));
			parentid = MapUtil.getStringValue(curMap, "id");
		}
		List curList = DistrictHelper.getSubDist(parentid);
		Iterator it = curList.iterator();
		while(it.hasNext()) {
			HashMap curdatamap = (HashMap)it.next();
			HashMap dataMap = new HashMap();
			dataMap.put("value", MapUtil.getStringValue(curdatamap, "id"));
			dataMap.put("option", MapUtil.getStringValue(curdatamap, "districtname"));
			list.add(dataMap);
		}
		
		return list;
	}
	
	public static String getSubDistOption(HashMap formMap, String parentid, String curid, boolean isfirst) {
		return getSubDistOption(formMap, parentid, MapUtil.getStringValue(formMap, curid), isfirst, false);
	}
	
	public static String getSubDistOption(HashMap formMap, String parentid, String curvalue, boolean isfirst, boolean islimit) {
		return getSubDistOption(formMap, parentid, curvalue, isfirst, islimit, "");
	}
	
	public static String getSubDistOption(HashMap formMap, String parentid, String curvalue, boolean isfirst, boolean islimit, String name) {
		StringBuffer str = new StringBuffer();
		if(!islimit)
			str.append("<option value=\"\">请选择</option>");
		if((StringUtil.isEmpty(parentid)) && (!isfirst))
			return str.toString();
		if(StringUtil.isEmpty(parentid)) {
			HashMap curMap = DistrictHelper.getDistByCode(Globals.getProperty("syssetting.defdistrictcode"));
			parentid = MapUtil.getStringValue(curMap, "id");
		}
		List curList = DistrictHelper.getSubDist(parentid);
		Iterator it = curList.iterator();
		boolean curflag = true;
		while(it.hasNext()) {
			HashMap curdatamap = (HashMap)it.next();
			String val = MapUtil.getStringValue(curdatamap, "id");
			if(islimit) {
				if(val.equals(curvalue)) 
					str.append("<option value=\"").append(val).append("\"").append(" selected=\"selected\"")
						.append(">").append(MapUtil.getStringValue(curdatamap, "districtname")).append("</option>");
			} else {
				str.append("<option value=\"").append(val).append("\"");
				if(val.equals(curvalue))
					str.append(" selected=\"selected\"");
				str.append(">").append(MapUtil.getStringValue(curdatamap, "districtname")).append("</option>");
			}
		}
		
		return str.toString();
	}
	
	public static String getDistNameByID(String id) {
		HashMap curMap = DistrictHelper.getDistByID(id);
		return MapUtil.getStringValue(curMap, "districtname", "&nbsp;");
	}
}
